<template>
  <div class="editorOrAdd">
    <el-dialog
      :title="title"
      :visible.sync="editorDialogVisible"
      width="50%" >
      <template>
        <div style="text-align: center">
          <avue-form v-if="isReload" :option="dialogOption" ref="resForm" v-model="formData" :upload-before="uploadBefore" :upload-after="uploadAfter">
            <template slot="returnCreatorName" >
              <el-select :disabled="showType==3" v-model="username" placeholder="请选择" style="width: 150px" @change="changeLine" filterable>
                <el-option
                  v-for="(item,index) in userList"
                  :key="index"
                  :label="item.username"
                  :value="item.userCode">
                </el-option>
              </el-select>
            </template>
            <template slot="returnCreatorCode">
              <el-input v-model="userCode" :disabled="true"></el-input>
            </template>
          </avue-form>
          <avue-crud
            ref="crud"
            :option="optionTwo"
            :data="listTwo">
            <template slot-scope="{row,size}" slot="otherTicketName">
              <el-input size="small" v-model="row.otherTicketName" :disabled="row.ticketType==0"></el-input>
            </template>
          </avue-crud>
          <br/>
          <template v-if="showType!=3">
            <el-button
              style="margin-top: 30px"
              class="filter-item"
              type="primary"
              size="mini"
              @click="submit(1)">保存</el-button>
          </template>
          <el-button
            style="margin-top: 30px"
            class="filter-item"
            type="primary"
            size="mini"
            @click="back()">返回</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import {getForm, postForm} from '@/api/gen/gen'
  import {mapGetters} from 'vuex';
  import moment from 'moment';
  import { handleDown } from '@/util/util';
  import store from '@/store'
  import {
    getLineName,
    getStationName,
    getSysDept,
    setSearchFiledsEmpty
  } from "../../../../../api/subwayRat/commonCheck";
  import transitUtil from "../../../../../api/subwayRat/transitUtil";
  import {
    update_xw_SwStApplyNetworkCheckPassengerAffairs,
    xw_cksw_form_Option,
    xw_cksw_optionTwo
  } from "../../../../../api/stationTiecketManager/shuJuXieChaGuanLi/SQXWXCCKSW/KZDLCK/editorOrAdd";
  import {getSysUserToLine} from "../../../../../api/stationTiecketManager/shuJuXieChaGuanLi/SQXWXCCKSW/KZDLCK/KZDLCK";
  export default {
    components: {},
    name: 'stationTransaction',
    props: {
      title: String
    },
    data() {
      return {
        showType:1, //1编辑，2添加，3预览
        userList : [],
        userCode : '',
        username : '',
        optionTwo: {},
        dialogOption: {},
        formData: {},
        listLoading: true,
        isReload: true,
        editorDialogVisible: false,
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条,
          isAsc: false// 是否倒序
        },

        list : [],
        listTwo : [],
        fileType : 0,
        originRows:{}
      }
    },
    computed: {
    },
    watch: {
    },
    created() {
    },
    methods: {
      setSearchFiledsEmpty,
      async show(row,showType){
        this.originRows=row;
        this.editorDialogVisible=true;
        this.showType=showType;

        this.dialogOption=xw_cksw_form_Option(this);
        this.optionTwo=xw_cksw_optionTwo(this);

        if(1 == showType){//编辑
          let resp =await getSysUserToLine({});
          this.userList = resp.data.data;
          this.username = this.$store.getters.userInfo.username;
          this.userCode = this.$store.getters.userInfo.userCode;
          let res=await getSysDept({deptId:row.deptId});
          this.formData=row;
          this.formData.deptName=res.data.data[0].name;
          let lineCode = parseInt(row.lineCode);
          this.formData.lineCode=getLineName(lineCode);
          this.formData.stationCode=getStationName(lineCode,row.stationCode);
          row.$cellEdit=true;
          this.listTwo=[row];
        }else if(3==showType){//预览
          this.formData=row;
          this.username = row.returnCreatorName;
          this.userCode = row.returnCreatorCode;
          let res=await getSysDept({deptId:row.deptId});
          this.formData.deptName=res.data.data[0].name;
          this.formData.lineCode=getLineName(row.lineCode);
          this.formData.stationCode=getStationName(row.lineCode,row.stationCode);
          row.$cellEdit=false;
          this.listTwo=[row];
        }else{
          for (var i in this.formData) {
            this.formData[i] = null;
          }
          let year=moment().format('YYYY');
          this.formData.transactionSerialNo=year;
          let res=await getSysDept({deptId:store.getters.userInfo.deptId});
          this.formData.deptName=res.data.data[0].name;
          this.formData.lineCode=getLineName(store.getters.userInfo.lineCode);
          this.formData.stationCode=getStationName(store.getters.userInfo.lineCode,store.getters.userInfo.stationCode);
          this.listTwo=[{$cellEdit:true}];
        }

      },
      uploadBefore(res, done, loading,column) {
          if("stationFile" == column.prop){
            this.fileType = 1;
          }else{
            this.fileType = 2;
          }
          done()
      },
      uploadAfter(res, done, loading,column) {
        done()
        let p = {
          id : this.formData.id
        };
        if(1 == this.fileType){
          p.stationFile = res.fileName
        }else{
          p.lineFile = res.fileName
        }
        // updateSwStApplyNetworkCheckPassengerAffairs(p).then((res)=>{
        // });
      },
      async submit(type){
        let flag = false;
        this.$refs.resForm.validate(vaild=>{
          if(vaild){
            flag = true;
          }
        })
        if(flag == true){
          for(let index in this.listTwo){
            const formCellRules = this.$refs.crud.formCellRules;
            let result = await this.asyncValidator(formCellRules, this.listTwo[index])
              .then(() => {return null})
              .catch(errors => {return errors})
            if(result){
              this.$message.info(`第${++index}行:${result[0].message}`)
              return
            }
          }
          let param={};
          Object.assign(param,this.formData);
            if(this.showType==1){
              let lineCode = store.getters.userInfo.lineCode;
              let stationCode = store.getters.userInfo.stationCode;
              this.originRows.lineCode = lineCode;
              this.originRows.stationCode = stationCode;
              this.update(this.originRows,param);
            }else if(this.showType==2){
              this.add(this.originRows,param);
            }
          Object.assign(param,this.listTwo[0]);
          update_xw_SwStApplyNetworkCheckPassengerAffairs(param).then((res)=>{
              if(res.data.code==0){
                this.$message.success("操作成功！");
                this.$parent.getList();
              }else{
                this.$message.error("操作失败！");
              }
            });
            this.editorDialogVisible = false;
        }
      },
      add(originrow,curr){
        curr.deptId=this.$store.getters.userInfo.deptId;
        curr.lineCode=this.$store.getters.userInfo.lineCode;
        curr.stationCode=this.$store.getters.userInfo.stationCode;
      },
      update(originrow,curr){
        curr.deptId=originrow.deptId;
        curr.lineCode=originrow.lineCode;
        curr.stationCode=originrow.stationCode;
      },
      changeLine(row){
          this.userCode = row;
      },
      back(){
          this.editorDialogVisible = false;
      },

      handleFilter(params, done) {
        Object.assign(this.searchForm,params);
        this.getList()
        done()
      },
      selectionChange(list){
        this.selectKeys=list;
        this.selectKeyVals=[];
        for(let i=0;i<list.length;i++){
          this.selectKeyVals.push(list[i].id);
        }
      }
    }
  }
</script>
<style lang="scss">
</style>
